using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace AppliedRecognition.Util
{
	/// <summary>
	/// Summary description for Filler.
	/// </summary>
	public class ShutdownConfig
	{
		private ShutdownConfig()
		{
		}


		[DllImport("Kernel32.dll",SetLastError = true)]
		private static extern bool SetProcessShutdownParameters(uint level, uint flags);

		[DllImport("kernel32.dll",SetLastError = true)]
		private static extern bool GetProcessShutdownParameters(out uint Level, out uint Flags);

		public static int ProcShutdownLevel 
		{
			get 
			{
				uint level, flags;
				if (!GetProcessShutdownParameters(out level, out flags))
				{
					throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to get process shutdown priority");
				}
				return (int) level;
			}
		}

		public static void SetMaxShutdownPriority()
		{
			if ( Environment.OSVersion.Version.Major < 5) return;
			for (uint ii=0x3ff; ii> 0x200;ii--)
			{
				if (SetProcessShutdownParameters(ii,1))
				{
					return;
				}
			}
			throw new Win32Exception(Marshal.GetLastWin32Error(),"Failed to set new process shutdown priority");
		}


		public static void SetMinShutdownPriority(int flags)
		{
			if ( Environment.OSVersion.Version.Major < 5) return;
			for (uint ii=0x200; ii< 0x2FF;ii++)
			{
				if (SetProcessShutdownParameters(ii,(uint)flags))
				{
					return;
				}
			}
			throw new Win32Exception(Marshal.GetLastWin32Error(),"Failed to set new process shutdown priority");
		}




	}//class
}//ns
